package org.liukai.tutorial.service.imp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.liukai.tutorial.dao.IAccountDao;
import org.liukai.tutorial.domain.Authority;
import org.liukai.tutorial.domain.Role;
import org.liukai.tutorial.domain.User;
import org.liukai.tutorial.service.IAccountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("accountService")
@Transactional
public class AccountService implements IAccountService {

	protected static Logger logger = Logger.getLogger("service");

	@Resource(name = "accountDao")
	private IAccountDao dao;

	/**
	 * 把从页面获得的 RoleCheckBox转换成数组格式.
	 */
	private Integer[] getRoleMenus(User user) {
		List<Object> list = new ArrayList<Object>();

		for (Map.Entry<Integer, Integer> map : user.getRoleCheckBox()
				.entrySet()) {
			if (map.getValue() != null) {
				list.add(map.getValue());
			}
		}
		return list.toArray(new Integer[] {});

	}

	/**
	 * 把从页面获得的 authCheckBox转换成数组格式.
	 */
	private Integer[] getAuthMenus(Role role) {
		List<Object> list = new ArrayList<Object>();

		for (Map.Entry<Integer, Integer> map : role.getAuthCheckBox()
				.entrySet()) {
			if (map.getValue() != null) {
				list.add(map.getValue());
			}
		}
		return list.toArray(new Integer[] {});

	}

	public User getUserInfo(String userName) {

		// 根据登录名获得对应的User信息
		User user = dao.getUserByLoginName(userName);

		// 根据登录名获得对应的角色(Role)信息.
		List<Role> roleList = dao.getRoleByUserName(userName);

		user.setRoleList(getAuthorityListByRoleList(roleList));

		return user;
	}

	public List<User> getAllUser() {

		List<User> userList = dao.getAllUser();

		List<User> list = new ArrayList<User>();

		for (int i = 0; i < userList.size(); i++) {

			// 将角色信息插入User中
			User user = getUserInfo(userList.get(i).getLoginName());

			list.add(user);
		}
		return list;
	}

	public User getUserById(Integer id) {
		User user = dao.getUserById(id);

		// 将角色信息插入User中retrun.
		return getUserInfo(user.getLoginName());
	}

	/**
	 * 根据RoleList中的ID获得对应的authority装入一个新的RoleList
	 */
	public List<Role> getAuthorityListByRoleList(List<Role> roleList) {

		List<Role> list = new ArrayList<Role>();

		for (int i = 0; i < roleList.size(); i++) {

			// 根据角色(Role)Id获得对应的权限(Authority)信息
			List<Authority> authorityList = dao.getAuthorityByRole(roleList
					.get(i).getId());

			Role role = new Role();

			role = roleList.get(i);

			role.setAuthorityList(authorityList);

			list.add(role);
		}

		return list;

	}

	public List<Role> getAllRole() {
		List<Role> roleList = dao.getAllRole();
		return getAuthorityListByRoleList(roleList);
	}

	public int deleteUser(Integer id) {
		dao.deleteUserRoleByUserId(id);
		return dao.deleteUser(id);
	}

	public int addUser(User user) {
		int i = dao.addUser(user);
		Integer userId = dao.getUserByLoginName(user.getLoginName()).getId();
		dao.addUserRole(userId, getRoleMenus(user));
		return i;
	}

	public int updateUser(User user) {
		dao.deleteUserRoleByUserId(user.getId());
		int i = dao.updateUser(user);
		dao.addUserRole(user.getId(), getRoleMenus(user));
		return i;
	}

	public List<Authority> getAllAuthority() {
		return dao.getAllAuthority();
	}

	public int addRole(Role role) {
		int i = dao.addRole(role);
		dao.addRoleAuthority(null, getAuthMenus(role));
		return i;
	}

	public int deleteRole(Integer id) {
		dao.deleteRoleAuthority(id);
		dao.deleteRoleUserByRoleId(id);
		return dao.deleteRole(id);
	}

	public Role getRoleById(Integer id) {

		// 根据RoleId获得对应的Role信息
		Role role = dao.getRoleById(id);
		role.setAuthorityList(dao.getAuthorityByRole(id));
		return role;
	}

	public int updateRole(Role role) {
		int i = dao.updateRole(role);
		dao.deleteRoleAuthority(role.getId());
		dao.addRoleAuthority(role.getId(), getAuthMenus(role));
		return i;
	}

	/**
	 * 验证登录名是否唯一
	 */
	public boolean isLoginNameUnique(String loginName, String oldLoginName) {

		if (loginName.equals(oldLoginName)) {
			return true;
		}

		User user = dao.getUserByLoginName(loginName);

		// true表示数据库没有相同的loginName
		return (user.getLoginName() == null);
	}

}
